package cn.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketService {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream io = null;
        OutputStream out = null;
        try {
            serverSocket = new ServerSocket(12345);
            //while (true){
                socket = serverSocket.accept();
                io = socket.getInputStream();
                byte[] data = new byte[1024];
                int len =0;
                out = socket.getOutputStream();

                while ((len = io.read(data)) != -1){
                    System.out.println(new String(data,0,len));
                }
            out.write("你好啊".getBytes());
            out.flush();
           //}
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(io != null){
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
